﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RainyPWS.Common;
using RainyPWS.Business;

namespace RainyPWS.Web.CommonHandler
{
    /// <summary>
    /// Summary description for RecordHandler
    /// </summary>
    public class RecordHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string action = MRequest.GetString("act");
            string htmlSource = string.Empty;
            switch (action)
            {
                case "updaterecordtype":
                    htmlSource = UpdateRecordType();
                    break;
                case "deleterecordtype":
                    htmlSource = DeleteRecordType();
                    break;
            }

            context.Response.ContentType = "text/plain";
            context.Response.Write(htmlSource);
        }

        private string UpdateRecordType()
        {
            int id = MRequest.GetInt("id", 0);
            int status = MRequest.GetInt("status", 0);
            if (id != 0 && status != -1)
            {
                int returnValue = BRecord.Instance.UpdateRecordType(id, status);
                if (returnValue > 0)
                    return "yes";
                else
                    return "no";
            }
            else
                return "no";
        }

        private string DeleteRecordType()
        {
            int id = MRequest.GetInt("id", 0);
            if (id == 0)
            {
                return "{\"result\":\"no\",\"msg\":\"参数错误\"}";
            }
            string msg = "";
            int result = BRecordType.Instance.DeleteRecordType(id, out msg);
            if (result > 0)
            {
                return "{\"result\":\"ok\",\"msg\":\"\"}";
            }
            else
            {
                return "{\"result\":\"no\", \"msg\":\"" + msg + "\"}";
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}